package com.example.view.event;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 自定义view用于测试
 */
public class MyView extends View {
    Paint mPaint;
    String TAG="MyView";
    public MyView(Context context) {
       this(context,null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData();
    }

    private void initData() {
        mPaint=new Paint();
        mPaint.setColor(Color.parseColor("#FF7165"));
        mPaint.setStrokeWidth(15);
       // mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        //canvas.drawRect(0,0,getWidth()/2,getHeight()/2,mPaint);
        //canvas.drawRect(0,0,getWidth()/2,getHeight()/2,mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return  super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN://如果当前view连down事件都不处理,那么后续他就不会收到其他事件
                getParent().requestDisallowInterceptTouchEvent(true);//请求父控件不要拦截事件,只要子控件在down事件的时候要求父控件不拦截,那么接下来的move和up事件都会交由子控件处理,在ACTION_MOVE和ACTION_UP中调用此方法不会生效
                Log.d(TAG,"MyView ACTION_DOWN");
                return true;
                //break;
            case MotionEvent.ACTION_MOVE:
                getParent().requestDisallowInterceptTouchEvent(false);//让父控件恢复过来
                Log.d(TAG,"MyView ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                //getParent().requestDisallowInterceptTouchEvent(false);
                Log.d(TAG,"MyView ACTION_UP");
                break;
        }
        //Log.d(TAG,"other thing");
        //Log.d(TAG,"onTouchEvent");
        return true;
    }

}
